package com.changgou.core;

import com.alibaba.fastjson.JSON;
import com.changgou.core.service.CoreService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

import java.util.List;
import java.util.Map;

/***
 * 描述
 * @author ljh
 * @packagename com.changgou.core
 * @version 1.0
 * @date 2020/8/10
 */

public abstract class AbstractCoreController<T> implements ICoreController<T> , WebRequestInterceptor {

    //调用方的service
    protected CoreService<T> coreService;
    //调用方的类型
    protected Class<T> clazz;

    protected Map<String, String> map;

    public AbstractCoreController(CoreService<T> coreService, Class<T> clazz) {
        this.coreService = coreService;
        this.clazz = clazz;
    }

    /**
     * 删除记录
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value="根据ID删除", notes="根据ID删除")
    @Override
    public Result deleteById(@PathVariable(name = "id") Object id) {
        coreService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 添加记录
     *
     * @param record
     * @return
     */
    @PostMapping
    @ApiOperation(value="添加数据", notes="添加数据")
    @Override
    public Result insert(@RequestBody T record) {
        coreService.insert(record);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 分页查询记录
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    @ApiOperation(value="无条件分页查询", notes="无条件分页查询")
    @Override
    public Result<PageInfo<T>> findByPage(@PathVariable(name = "page") Integer pageNo,
                                          @PathVariable(name = "size") Integer pageSize) {
        PageInfo<T> pageInfo = coreService.findByPage(pageNo, pageSize);
        return new Result<PageInfo<T>>(true, StatusCode.OK, "分页查询成功", pageInfo);
    }

    @PostMapping(value = "/search/{page}/{size}")
    @ApiOperation(value="有条件分页查询", notes="有条件分页查询")
    @Override
    public Result<PageInfo<T>> findByPage(@PathVariable(name = "page") Integer pageNo,
                                          @PathVariable(name = "size") Integer pageSize,
                                          @RequestBody T record) {
        PageInfo<T> pageInfo = coreService.findByPage(pageNo, pageSize, record);
        return new Result<PageInfo<T>>(true, StatusCode.OK, "条件分页查询成功", pageInfo);
    }

    @Override
    @GetMapping("/{id}")
    @ApiOperation(value="根据ID查询数据", notes="根据ID查询数据")
    public Result<T> findById(@PathVariable(name = "id") Object id) {
        T t = coreService.selectByPrimaryKey(id);
        return new Result<T>(true, StatusCode.OK, "查询单个数据成功", t);
    }

    @Override
    @GetMapping
    @ApiOperation(value="查询所有数据", notes="查询所有数据")
    public Result<List<T>> findAll() {
        List<T> list = coreService.selectAll();
        return new Result<List<T>>(true, StatusCode.OK, "查询所有数据成功", list);
    }

    //更新数据
    @Override
    @PutMapping
    @ApiOperation(value="根据主键更新数据", notes="根据主键更新数据")
    public Result updateByPrimaryKey(@RequestBody T record) {
        coreService.updateByPrimaryKey(record);
        return new Result(true, StatusCode.OK, "更新成功");
    }

    private static final String AUTHORIZE_TOKEN_HEADER = "Authorization";
    @Override
    public void preHandle(WebRequest request) throws Exception {
        //处理器执行之前 执行
        /*ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            //获取登录的用户名 value 是一个字符串{“username_name”:"zhangsna"。。。。。。。}
            String value = requestAttributes.getRequest().getHeader(AUTHORIZE_TOKEN_HEADER);
            if (!StringUtils.isEmpty(value)) {
                map = JSON.parseObject(value, Map.class);
            }
        }*/

        String value =request.getHeader(AUTHORIZE_TOKEN_HEADER);
        if (!StringUtils.isEmpty(value)) {
            map = JSON.parseObject(value, Map.class);
        }

    }

    @Override
    public void postHandle(WebRequest request, ModelMap model) throws Exception {

        //处理器中执行
    }

    @Override
    public void afterCompletion(WebRequest request, Exception ex) throws Exception {

        //处理器执行之后执行
    }
}
